package com.ytd.smartcanteen.weighting.fragment;

import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.Nullable;

import com.ytd.smartcanteen.weighting.bean.MealInfo;
import com.ytd.smartcanteen.weighting.util.MealInfoUtil;

import cn.ytd.smartcanteen.weighting.R;
import cn.ytd.smartcanteen.weighting.databinding.FragmentNormalBinding;


/**
 * 默认显示的产品信息
 *
 * @author gaoguanchao
 * @since 2023/4/18
 */
public class NormalFragment extends BaseFragment {

    private MealInfo mMealInfo;
    private FragmentNormalBinding mBinding;

    @Override
    protected void initData(View view) {
        refreshUi();
    }

    @Override
    protected void initView(View view) {
        mBinding = FragmentNormalBinding.bind(view);
        Drawable drawable = mBinding.ivShowImage.getDrawable();
        if (drawable instanceof AnimationDrawable) {
            ((AnimationDrawable) drawable).start();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_normal;
    }

    public void setFoodInfo(@Nullable MealInfo result) {
        mMealInfo = result;
        if (result == null) {
            showEntryView();
            return;
        }
        changeViewData();
    }

    private CharSequence getPrice() {
        String priceFormat = MealInfoUtil.getPriceFormat(Double.parseDouble(mMealInfo.price));
        return priceFormat + " " + mMealInfo.priceUnit;
    }

    @SuppressLint("SetTextI18n")
    private void changeViewData() {
        if (mBinding == null) {
            return;
        }
        mBinding.tvFoodName.setText(mMealInfo.foodName);
        mBinding.tvMealTimeName.setText(mMealInfo.mealTimeName);
        CharSequence priceUnit = getPrice();
        String weightUnit = MealInfoUtil.getWeightUnit(mMealInfo);
        mBinding.tvWaitText.setText(mMealInfo.mealStartTime + "-" + mMealInfo.mealEndTime);
        mBinding.tvPriceUnit.setText(priceUnit);
        mBinding.tvWeightUnit.setText(weightUnit);
        if (mMealInfo.inMeal) {
            mBinding.tvMealTimeName.setText(
                mMealInfo.mealTimeName + " (" + mMealInfo.mealStartTime + "-" + mMealInfo.mealEndTime + ")");
            mBinding.ivShowImage.setVisibility(View.VISIBLE);
            mBinding.llWait.setVisibility(View.GONE);
        } else {
            mBinding.ivShowImage.setVisibility(View.GONE);
            mBinding.llWait.setVisibility(View.VISIBLE);
        }

        if (mMealInfo.recipeInfo != null) {
            mBinding.llBottom.setVisibility(View.VISIBLE);
            mBinding.tvEnergy.setText(String.format(getString(R.string.text_heat),
                MealInfoUtil.getValueFormat(mMealInfo.recipeInfo.energy)));
            mBinding.tvProtein.setText(String.format(getString(R.string.text_protein),
                MealInfoUtil.getValueFormat(mMealInfo.recipeInfo.protein)));
            mBinding.tvFat.setText(String.format(getString(R.string.text_fat),
                MealInfoUtil.getValueFormat(mMealInfo.recipeInfo.fat)));
            mBinding.tvCarbohydrate.setText(String.format(getString(R.string.text_carbohydrate),
                MealInfoUtil.getValueFormat(mMealInfo.recipeInfo.carbohydrate)));
        } else {
            mBinding.llBottom.setVisibility(View.GONE);
        }
    }

    private void showEntryView() {
        if (mBinding == null) {
            return;
        }
        mBinding.tvFoodName.setText("");
        mBinding.tvMealTimeName.setText("");
        mBinding.tvWaitText.setText("");
        mBinding.tvPriceUnit.setText("");
        mBinding.tvWeightUnit.setText("");
        mBinding.ivShowImage.setVisibility(View.VISIBLE);
        mBinding.llWait.setVisibility(View.GONE);
        mBinding.llBottom.setVisibility(View.GONE);
    }

    public void refreshUi() {
        if (mMealInfo == null) {
            showEntryView();
            return;
        }
        changeViewData();
    }
}
